import QtQuick 2.15
import UIModule 1.0
Item {
    id: root
    // implicitWidth: row.width
    // implicitHeight: row.height
    property alias hour: hourItem.str
    property alias minute: minuteItem.str
    property alias second: secondItem.str
    property bool hideSecondEn: false
    property bool h24: true
    property int cvalue: hideSecondEn ? 3 : 2
    property int tHW: ((width - 50 * 2 - 20 * (cvalue - 1)) / (cvalue))*0.9
                      < (height - 2 * 20) ? (width - 50 * 2 - 20 * (cvalue - 1))
                                            / (cvalue) : (height - 2 * 20)
    property alias spac: row.spacing
    Row {
        id: row
        y: (root.height - hourItem.height) / 2
        x: (root.width - hourItem.width * cvalue - spac * (cvalue - 1)) / 2
        width: tHW * (3) + spac * (2)
        height: width
        spacing: 20
        TimeItem {
            id: hourItem
            width: tHW
            height: tHW * 0.9
            Text{
                anchors.bottom: parent.bottom
                anchors.right: parent.right
                anchors.bottomMargin: parent.width*0.02
                anchors.rightMargin: parent.width*0.05

                text:(new Date).getHours()<12?"AM":"PM"
                font.pixelSize: tHW*0.1
                font.bold: true
                visible: UI.showAPMEn
                color: UI.textColor
            }
        }
        TimeItem {
            id: minuteItem
            width: tHW
            height: tHW * 0.9
        }
        TimeItem {
            id: secondItem
            width: tHW
            height: tHW * 0.9
            visible: hideSecondEn
        }
    }
    Timer{
        id:timerItem
        repeat: true
        interval: 990
        onTriggered: {
            var time = new Date
            var h = time.getHours()
            if(!h24) h = h>12?h-12:h
            root.hour = h.toString().padStart(2, '0')
            root.minute = time.getMinutes().toString().padStart(2, '0')
            root.second =time.getSeconds().toString().padStart(2, '0')
        }

    }

    Component.onCompleted: {
        var time = new Date
        var h = time.getHours()
        if(h24) h = h>12?h-12:h
        root.hour = h.toString().padStart(2, '0')
        root.minute = time.getMinutes().toString().padStart(2, '0')
        root.second = time.getSeconds().toString().padStart(2, '0')
        timerItem.start()
    }
}
